package com.yupi.yuojcodesandbox.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yuhao
 * @Date: 2025/3/13 21:20
 */
@Configuration
public class ThreadPoolsConfig {
    //ExecutorService  适用于 需要按任务提交顺序获取结果的场景
    private static
    @Bean
    ThreadPoolExecutor threadPoolExecutor() {
        int cpuCores = Runtime.getRuntime().availableProcessors();
        return new ThreadPoolExecutor(
                cpuCores,
                cpuCores * 2,
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

}
